package com.doform.android.xianggucheng.popwindow;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.adapter.CommonListAdapter;

/**
 * Created by Administrator on 2017/5/13.
 */

public class IPopWindow extends PopupWindow {

    private Context mContext;
    private ListView mListView;
    private int mLayoutId;
    private TextView tvDead;
    private RelativeLayout rlHead;
    private View headView;
    private View mConvertView;

    public IPopWindow(Context context, int mLayoutId) {
        super(context);
        this.mContext = context;
        this.mLayoutId = mLayoutId;
        initView();
    }

    private SparseArray<View> mViews;

    private void initView() {
        mViews = new SparseArray<>();
        mConvertView = LayoutInflater.from(mContext).inflate(mLayoutId, null);
        headView = LayoutInflater.from(mContext).inflate(R.layout.layout_pop_head, null);
        rlHead = (RelativeLayout) headView.findViewById(R.id.rl);
        tvDead = (TextView) headView.findViewById(R.id.tv);

            mListView = (ListView) mConvertView.findViewById(R.id.listview);
            mListView.addHeaderView(headView);

        mConvertView.findViewById(R.id.ll).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IPopWindow.this.dismiss();
            }
        });
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setOutsideTouchable(true);
        this.setContentView(mConvertView);
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));
    }

    public void setHeadListener(View.OnClickListener onClickListener) {
        rlHead.setOnClickListener(onClickListener);
    }

    public View getHeadView() {
        return headView;
    }

    public void setHeadName(String name) {
        tvDead.setText(name);
    }

    public void setPopAdapter(CommonListAdapter commonListAdapter) {
            mListView.setAdapter(commonListAdapter);
    }

    public void setGoneHeadView() {
        headView.setVisibility(View.GONE);
        mListView.removeHeaderView(headView);
    }

    public <T extends View> T getView(int viewId) {

        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }
}
